package cn.iocoder.lw.module.core.controller.admin.workorder;

import cn.iocoder.lw.framework.common.pojo.CommonResult;
import cn.iocoder.lw.framework.common.pojo.PageResult;
import cn.iocoder.lw.framework.common.pojo.PageParam;
import cn.iocoder.lw.framework.excel.core.util.ExcelUtils;
import cn.iocoder.lw.module.core.controller.admin.workorder.vo.report.WorkOrderReportGenerateReqVO;
import cn.iocoder.lw.module.core.controller.admin.workorder.vo.report.WorkOrderReportPageReqVO;
import cn.iocoder.lw.module.core.controller.admin.workorder.vo.report.WorkOrderStatisticsRespVO;
import cn.iocoder.lw.module.core.controller.admin.workorder.vo.report.WorkOrderOverallStatisticsRespVO;
import cn.iocoder.lw.module.core.service.workorder.WorkOrderReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.lw.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 工单报表")
@RestController
@RequestMapping("/core/work-order-report")
@Validated
@Slf4j
public class WorkOrderReportController {

    @Resource
    private WorkOrderReportService workOrderReportService;

    @PostMapping("/generate")
    @Operation(summary = "生成工单报表统计")
    @PreAuthorize("@ss.hasPermission('core:work-order-report:generate')")
    public CommonResult<List<WorkOrderStatisticsRespVO>> generateWorkOrderReport(@Valid @RequestBody WorkOrderReportGenerateReqVO generateReqVO) {
        List<WorkOrderStatisticsRespVO> reportList = workOrderReportService.generateWorkOrderReport(generateReqVO);
        return success(reportList);
    }

    @GetMapping("/page")
    @Operation(summary = "获取工单报表统计分页")
    @PreAuthorize("@ss.hasPermission('core:work-order-report:query')")
    public CommonResult<PageResult<WorkOrderStatisticsRespVO>> getWorkOrderReportPage(@Valid WorkOrderReportPageReqVO pageReqVO) {
        PageResult<WorkOrderStatisticsRespVO> pageResult = workOrderReportService.getWorkOrderReportPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/user-statistics")
    @Operation(summary = "获取用户工单详情统计")
    @PreAuthorize("@ss.hasPermission('core:work-order-report:query')")
    public CommonResult<WorkOrderStatisticsRespVO> getUserWorkOrderReport(
            @Parameter(description = "用户ID", required = true, example = "1")
            @RequestParam("userId") Long userId,
            @Parameter(description = "报表类型", required = true, example = "1")
            @RequestParam("reportType") Integer reportType,
            @Parameter(description = "用户类型", example = "1")
            @RequestParam(value = "userType", required = false) Integer userType,
            @Parameter(description = "开始时间", required = true)
            @RequestParam("startTime") LocalDateTime startTime,
            @Parameter(description = "结束时间", required = true)
            @RequestParam("endTime") LocalDateTime endTime) {
        WorkOrderStatisticsRespVO statistics = workOrderReportService.getUserWorkOrderReport(userId, reportType, userType, startTime, endTime);
        return success(statistics);
    }

    @GetMapping("/overall-statistics")
    @Operation(summary = "获取工单总体统计")
    @PreAuthorize("@ss.hasPermission('core:work-order-report:query')")
    public CommonResult<WorkOrderOverallStatisticsRespVO> getOverallStatistics(
            @Parameter(description = "开始时间", required = true)
            @RequestParam("startTime") LocalDateTime startTime,
            @Parameter(description = "结束时间", required = true)
            @RequestParam("endTime") LocalDateTime endTime) {
        WorkOrderOverallStatisticsRespVO statistics = workOrderReportService.getOverallStatistics(startTime, endTime);
        return success(statistics);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单报表Excel")
    @PreAuthorize("@ss.hasPermission('core:work-order-report:export')")
    public void exportWorkOrderReportExcel(@Valid WorkOrderReportPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkOrderStatisticsRespVO> data = workOrderReportService.getWorkOrderReportPage(pageReqVO).getList();

        // 导出 Excel
        ExcelUtils.write(response, "工单报表统计.xls", "数据", WorkOrderStatisticsRespVO.class, data);
    }

}